//
//  YCalendarView.m
//  TestCalendar
//
//  Created by yull on 14/12/2.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "YCalendarView.h"
#import "UIView+Frame.h"
#import "CalendarLogic.h"

@implementation YCalendarView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
        [self initTopView];
        
        [self initMonthView];
        
        [self initDay];
        
        [self refreshCanlendar];
        
        self.height = self.monthView.bottom;
    }
    return self;
}

- (void)initTopView
{
    CGRect rect = CGRectMake(0, 0, self.width, 70);
    self.topView = [[UIView alloc] initWithFrame:rect];
    [self addSubview:self.topView];
    
    rect = CGRectMake(20, 5, 36, 20);
    self.preButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.preButton.frame = rect;
    self.preButton.backgroundColor = [UIColor clearColor];
    [self.preButton setImage:[UIImage imageNamed:@"shangyue"] forState:UIControlStateNormal];
    [self.topView addSubview:self.preButton];
    
    rect.origin.x = self.topView.width - self.preButton.right;
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = rect;
    self.nextButton.backgroundColor = [UIColor clearColor];
    [self.nextButton setImage:[UIImage imageNamed:@"xiayue"] forState:UIControlStateNormal];
    [self.topView addSubview:self.nextButton];
    
    self.topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.topView.width, 30)];
    self.topTitleLabel.textColor = [UIColor blackColor];
    self.topTitleLabel.backgroundColor = [UIColor clearColor];
    self.topTitleLabel.font = [UIFont systemFontOfSize:16.0f];
    self.topTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.topView addSubview:self.topTitleLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(5, self.topTitleLabel.height - 1.0f, self.topTitleLabel.width-10, 1.0f)];
    lineView.backgroundColor = RGB(239, 193, 153);
    [self.topView addSubview:lineView];
    
    CGFloat width = self.width / 7;
    for (NSInteger i = 0; i<7; i++) {
        
        CGRect rect = CGRectMake(i*width, self.topTitleLabel.bottom+10, width, 20);
        UILabel *label = [[UILabel alloc] initWithFrame:rect];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [self getWeekStr:i];
        [self.topView addSubview:label];
    }
    
    lineView = [[UIView alloc] initWithFrame:CGRectMake(5, self.topView.height - 1.0f, self.topView.width-10, 1.0f)];
    lineView.backgroundColor = RGB(239, 193, 153);
    [self.topView addSubview:lineView];
    
    [self.preButton addTarget:self action:@selector(onPreButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.nextButton addTarget:self action:@selector(onNextButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initMonthView
{
    CGRect rect = CGRectMake(0, self.topView.bottom, self.width, 0);
    
    self.monthView = [[PerMonthView alloc] initWithFrame:rect];
    self.monthView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.monthView.delegate = self;
    self.monthView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.monthView];
}

- (void)initDay
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    self.currentMonth = components.month;
    self.currentYear = components.year;
}

- (void)refreshCanlendar
{
    self.monthView.dayCount = [CalendarLogic getDayCountInYear:self.currentYear month:self.currentMonth];
    self.monthView.firstDay = [CalendarLogic getFirstWeekInYear:self.currentYear month:self.currentMonth];
    self.currentWeekCount = [CalendarLogic getWeekCountInYear:self.currentYear month:self.currentMonth];
    
    [self.monthView reloadData];
    
    [self updateTitle];
    
    [self refreshFrame];
}

- (void)updateTitle
{
    NSString *str = [NSString stringWithFormat:@" %d 年 %02d 月",self.currentYear,self.currentMonth];
    self.topTitleLabel.text = str;
}

- (void)refreshFrame
{
    self.monthView.top = self.topView.bottom;
    self.monthView.height = self.currentWeekCount * self.monthView.itemSize.height + 5;
    if ([self.delegate respondsToSelector:@selector(calendarViewChangeHeight:)]) {
        [self.delegate calendarViewChangeHeight:self.monthView.bottom];
    }
}

- (void)setCurrentMonth:(NSInteger)currentMonth
{
    if (currentMonth > 12) {
        currentMonth = 1;
        self.currentYear ++;
    }else if (currentMonth <= 0) {
        currentMonth = 12;
        self.currentYear --;
    }
    
    _currentMonth = currentMonth;
}

- (void)onPreButtonClicked:(UIButton *)button
{
    self.currentMonth --;
    
    if ([self.delegate respondsToSelector:@selector(onClickPreButton)]) {
        [self.delegate onClickPreButton];
        
        [self refreshCanlendar];
        
        [self updateTitle];
    }
}

- (void)onNextButtonClicked:(UIButton *)button
{
    self.currentMonth ++;
    
    if ([self.delegate respondsToSelector:@selector(onClickNextButton)]) {
        [self.delegate onClickNextButton];
        
        [self refreshCanlendar];
        
        [self updateTitle];
    }
}

- (void)clickAtDay:(NSInteger)day
{
    if ([self.delegate respondsToSelector:@selector(didSelectMonth:day:)]) {
        [self.delegate didSelectMonth:self.currentMonth day:day];
    }
}

- (void)insertFailed:(NSString *)errorMsg
{
    if ([self.delegate respondsToSelector:@selector(insertFailed:)]) {
        [self.delegate insertFailed:errorMsg];
    }
}

#pragma mark - Private
- (NSString *)getWeekStr:(NSInteger)i
{
    switch (i) {
        case 0:
            return @"周一";
        case 1:
            return @"周二";
        case 2:
            return @"周三";
        case 3:
            return @"周四";
        case 4:
            return @"周五";
        case 5:
            return @"周六";
        default:
            return @"周日";
    }
}
@end
